package com.lwh.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwh.yygh.hosp.repository.HospitalRepository;
import com.lwh.yygh.hosp.service.HospitalApiService;
import com.lwh.yygh.model.hosp.Hospital;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @author lwh
 * @date 2021/5/21 19:59
 */
@Service
public class HospitalApiServiceImpl implements HospitalApiService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Override
    public void save(Map<String, Object> map) {
        //把参数map转换成一个对象Hospital，方便操作
        String mapString = JSONObject.toJSONString(map);
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);
        //判断是否存在数据
        String hoscode = hospital.getHoscode();
        Hospital hospitalExist =hospitalRepository.getHospitalByHoscode(hoscode);
        //如果不存在，进行添加
        if (hospitalExist != null) {
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setUpdateTime(new Date());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }else {
            //如果存在，进行修改
            hospital.setStatus(0);
            hospital.setUpdateTime(new Date());
            hospital.setCreateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }


    }
}
